<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //   var obj1 = { name: 'jyd', age: 25 }
      //   var obj2 = {} // 新空间

      //   // 循环旧对象把属性赋值给obj2
      //   for (var k in obj1) {
      //     obj2[k] = obj1[k]
      //   }

      //   console.log(obj2)
      //   console.log(obj1 === obj2) // false

      //   封装函数：实现对对象的深拷贝（一层）
      function copyDeep(obj) {
        // 循环旧对象把属性赋值给result
        var result = {}
        for (var k in obj) {
          result[k] = obj[k]
        }
        return result
      }
      var obj1 = { name: 'jyd', age: 25 }
      var obj2 = copyDeep(obj1)
      console.log(obj1);
      console.log(obj2);
      console.log(obj1 === obj2) // false
    </script>
  </body>
</html>
